/*
 * PEntidad.java
 *
 * Created on 30 de agosto de 2006, 12:39 PM
 */

package ig.formato.gui.app;

import ig.formato.modelo.MEntidad;
import ig.basic.util.IMetodosPantallas;
import ig.formato.basic.ManipulacionDatos;
import ig.formato.dto.VEntidadDTO;
import ig.formato.util.IGFormatoException;
import ig.formato.util.NinguDatoSeleccionadoException;
import ig.formato.util.OBJETOS;
import ig.formato.util.TModelCamposEntidad;
import ig.formato.util.ValueDocument;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;

/**
 *
 * @author  jsanchez
 */
public class PEntidad extends javax.swing.JPanel  implements IMetodosPantallas{
    private ManipulacionDatos mpd;
    MEntidad modelo=null;
    /** Creates new form PEntidad */
    JFrame frame=null;
    public PEntidad(JFrame frame) {
        initComponents();
        this.frame=frame;
        mpd = new ManipulacionDatos(true,false);
        mpd.setConfimacioSalir(true,"Desea cerrar la ventana?");
        mpd.setPanelMetodosPantalla(this);
        mpd.setSize(600,500);
        mpd.setTitle("Datos de la corporaci\u00F3n");
        //mpd.setResizable(false);
        //mpd.setMaximizable(false);
        modelo= new MEntidad(this);
        //TGrupoPoratafolio.setDocument(new ValueDocument("INTEGER"));
        TCodigo.setDocument(new ValueDocument("INTEGER"));
        TTipo.setDocument(new ValueDocument("INTEGER"));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        TNombre = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        TCodigo = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        TTipo = new javax.swing.JTextField();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel5 = new javax.swing.JLabel();
        TClave = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        TCampos = new javax.swing.JTable();
        jLabel6 = new javax.swing.JLabel();
        Nuevo = new javax.swing.JButton();
        Eliminar = new javax.swing.JButton();

        jLabel1.setFont(new java.awt.Font("Dialog", 1, 14));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText(org.openide.util.NbBundle.getMessage(PEntidad.class, "PEntidad.jLabel1.text")); // NOI18N

        jLabel2.setFont(new java.awt.Font("Dialog", 0, 10));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel2.setText(org.openide.util.NbBundle.getMessage(PEntidad.class, "PEntidad.jLabel2.text")); // NOI18N

        TNombre.setFont(new java.awt.Font("Dialog", 0, 10));
        TNombre.setText(org.openide.util.NbBundle.getMessage(PEntidad.class, "PEntidad.TNombre.text")); // NOI18N
        TNombre.setDisabledTextColor(new java.awt.Color(0, 0, 0));

        jLabel3.setFont(new java.awt.Font("Dialog", 0, 10));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel3.setText(org.openide.util.NbBundle.getMessage(PEntidad.class, "PEntidad.jLabel3.text")); // NOI18N

        TCodigo.setFont(new java.awt.Font("Dialog", 0, 10));
        TCodigo.setText(org.openide.util.NbBundle.getMessage(PEntidad.class, "PEntidad.TCodigo.text")); // NOI18N
        TCodigo.setDisabledTextColor(new java.awt.Color(0, 0, 0));

        jLabel4.setFont(new java.awt.Font("Dialog", 0, 10));
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jLabel4.setText(org.openide.util.NbBundle.getMessage(PEntidad.class, "PEntidad.jLabel4.text")); // NOI18N

        TTipo.setFont(new java.awt.Font("Dialog", 0, 10));
        TTipo.setText(org.openide.util.NbBundle.getMessage(PEntidad.class, "PEntidad.TTipo.text")); // NOI18N
        TTipo.setDisabledTextColor(new java.awt.Color(0, 0, 0));

        jLabel5.setFont(new java.awt.Font("Dialog", 0, 10));
        jLabel5.setText(org.openide.util.NbBundle.getMessage(PEntidad.class, "PEntidad.jLabel5.text")); // NOI18N

        TClave.setFont(new java.awt.Font("Dialog", 0, 10));
        TClave.setText(org.openide.util.NbBundle.getMessage(PEntidad.class, "PEntidad.TClave.text")); // NOI18N
        TClave.setDisabledTextColor(new java.awt.Color(0, 0, 0));

        TCampos.setModel( new TModelCamposEntidad(
            new Object [][] {},
            new String [] {"Nombre", "Valor"},
            new boolean [] {true, true}
        ));
        jScrollPane1.setViewportView(TCampos);

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText(org.openide.util.NbBundle.getMessage(PEntidad.class, "PEntidad.jLabel6.text")); // NOI18N

        Nuevo.setText(org.openide.util.NbBundle.getMessage(PEntidad.class, "PEntidad.Nuevo.text")); // NOI18N
        Nuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NuevoActionPerformed(evt);
            }
        });

        Eliminar.setText(org.openide.util.NbBundle.getMessage(PEntidad.class, "PEntidad.Eliminar.text")); // NOI18N
        Eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                EliminarActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE)
                    .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel5, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(TClave, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
                            .add(TCodigo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
                            .add(TNombre, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
                            .add(TTipo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)))
                    .add(layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(Eliminar, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(Nuevo, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 33, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(TNombre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(TCodigo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(TTipo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(TClave, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel5))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jLabel6)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(Nuevo)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(Eliminar))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void NuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NuevoActionPerformed
// TODO add your handling code here:
    Collection<VEntidadDTO> cdtos = this.loadDataOnTable();
    cdtos.add(new VEntidadDTO("", ""));
    this.showDataOnTable(cdtos);    
    
}//GEN-LAST:event_NuevoActionPerformed

private void EliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EliminarActionPerformed
// TODO add your handling code here:
    if (this.TCampos.getSelectedRow() >= 0) {
        ((TModelCamposEntidad) this.TCampos.getModel()).delete(this.TCampos.getSelectedRow());
        this.TCampos.updateUI();
    }
    
}//GEN-LAST:event_EliminarActionPerformed
    

    public Collection<VEntidadDTO> loadDataOnTable() {
        Object[][] data = ((TModelCamposEntidad) this.TCampos.getModel()).getObjet();
        Collection<VEntidadDTO> dTOs = new ArrayList<VEntidadDTO>();
        for (int i = 0; i < data.length; i++) {
            dTOs.add((VEntidadDTO) data[i][0]);
        }
        return dTOs;
    }

    public void showDataOnTable(Collection<VEntidadDTO> c)  {
        Object[][] data = null;
        if (c != null) {
            data = new Object[c.size()][2];
            int i = 0;
            for (VEntidadDTO dto : c) {
                data[i][0] = dto;
                data[i++][1] = dto.getValor();
            }
        }
        ((TModelCamposEntidad) this.TCampos.getModel()).setData(data);
        this.TCampos.updateUI();
    }
    
    public JInternalFrame getInternalFrame() {
        return this.mpd;
    }
    
    public void validar() throws IGFormatoException {
        if(this.TNombre.getText().trim().equals(""))
            throw new ig.formato.util.IGFormatoException("No se escribio un nombre","Escriba el nombre de.");
        
        if(this.TCodigo.getText().trim().equals(""))
            throw new ig.formato.util.IGFormatoException("No se escribio un Codigo","Escriba el Codigo.");
        
        if(this.TTipo.getText().trim().equals(""))
            throw new ig.formato.util.IGFormatoException("No se escribio un Tipo","Escriba el Tipo.");
        
        if(this.TClave.getText().trim().equals(""))
            throw new ig.formato.util.IGFormatoException("No se escribio la palabra calve","Escriba la palabra calve.");
        
        for (VEntidadDTO dTO : this.loadDataOnTable()) {
            if(dTO.getNombre()==null || dTO.getValor()==null || dTO.getNombre().trim().equals("") || dTO.getValor().trim().equals("")){
                throw new ig.formato.util.IGFormatoException("Algunos campos no tienen nombre o valor.","Agregele su valor correspondiente");
            }            
        }        
        
/*        if(this.TGrupoPoratafolio.getText().trim().equals(""))
            throw new ig.formato.util.IGFormatoException("No se escribio un Grupo de Portafolio","Escriba el Grupo de Portafolio");*/
        
//        try{
//            ((ig.formato.dto.EntidadDTO)modelo.getTO()).setIdConexion(((ig.formato.dto.ConexionDTO)this.CBConexiones.getSelectedItem()).getId());
//        }catch(Exception e){
//            e.printStackTrace();
//            throw new ig.formato.util.IGFormatoException(e,"No se pudo resolver la Fuente de Datos.");
//        }
        
        
        ((ig.formato.dto.EntidadDTO)modelo.getTO()).setNombre(this.TNombre.getText().trim());
        ((ig.formato.dto.EntidadDTO)modelo.getTO()).setCodigo(this.TCodigo.getText().trim());
//        ((ig.formato.dto.EntidadDTO)modelo.getTO()).setGrupoPortafolio(Integer.valueOf(this.TGrupoPoratafolio.getText()));
        ((ig.formato.dto.EntidadDTO)modelo.getTO()).setPalabraClave(this.TClave.getText().trim());
//        ((ig.formato.dto.EntidadDTO)modelo.getTO()).setPatrimonioFideicomiso(this.CPatrimonioFideicomiso.isSelected()?1:0);
        ((ig.formato.dto.EntidadDTO)modelo.getTO()).setTipo(this.TTipo.getText().trim());
    }
    
    public void guardar() throws IGFormatoException {
        modelo.guardar();
        this.actualizar();
    }
    
    public void buscar() throws IGFormatoException, NinguDatoSeleccionadoException {
        DBusqueda dialog=new DBusqueda(this.getFrame(),this.modelo,modelo.buscar());
        dialog.setVisible(true);
        if(dialog.getOpcion().equals(OBJETOS.NO_SELECCION)){
            throw new ig.formato.util.NinguDatoSeleccionadoException();
        }else{
            this.actualizar();
        }
    }
    
    public void modificar() throws IGFormatoException {
        modelo.modificar();
    }
    
    public void nuevo() throws IGFormatoException {
//        this.CBConexiones.removeAllItems();        
        modelo.nuevo();
//        this.actualizarCombo();        
    }
    
    public void eliminar() throws IGFormatoException {
        modelo.eliminar();
    }
    
    public void salir() throws IGFormatoException {
        modelo.salir();
//        this.CBConexiones.removeAllItems();
//        this.CBConexiones.removeAll();
    }
    
    public void imprimir() throws IGFormatoException {
        this.mpd.parametros.put("JASPER","Entidad");
    }
    public Map getParametros() throws IGFormatoException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public void limpiar() throws IGFormatoException {
    
    }
    public ig.basic.util.IModelo getModelo() throws IGFormatoException {
        return this.modelo;
    }
    public void cancelar() throws IGFormatoException {
        modelo.cancelar();
        modelo.buscar();
        this.actualizar();
    }
//    private void actualizarCombo() throws IGFormatoException{
//        this.CBConexiones.removeAllItems();
//        Object obj=null;
//        for (Object elem : modelo.getFunetesDeDatos()) {
//            this.CBConexiones.addItem(elem);
//            if(((ig.formato.dto.ConexionDTO)elem).getId().equals(((ig.formato.dto.EntidadDTO)modelo.getTO()).getIdConexion()))
//                obj=elem;
//        }
//        if(obj!=null){
//            this.CBConexiones.setSelectedItem(obj);
//        }else if(CBConexiones.getItemCount()>0)
//            this.CBConexiones.setSelectedIndex(0);
//    }
    
    public void actualizar() throws IGFormatoException {
//        this.actualizarCombo();
        if(((ig.formato.dto.EntidadDTO)modelo.getTO()).getId()!=null){
            this.TNombre.setText(((ig.formato.dto.EntidadDTO)modelo.getTO()).getNombre());
            this.TCodigo.setText(((ig.formato.dto.EntidadDTO)modelo.getTO()).getCodigo());
//            this.TGrupoPoratafolio.setText(((ig.formato.dto.EntidadDTO)modelo.getTO()).getGrupoPortafolio().toString());
            this.TClave.setText(((ig.formato.dto.EntidadDTO)modelo.getTO()).getPalabraClave());
//            this.CPatrimonioFideicomiso.setSelected(((ig.formato.dto.EntidadDTO)modelo.getTO()).getPatrimonioFideicomiso()==1?true:false);
            this.TTipo.setText(((ig.formato.dto.EntidadDTO)modelo.getTO()).getTipo());
        }
    }
    
    public Component[] getControles() {
        return this.getComponents();
    }
    
    public void setFrame(JFrame frame) {
        this.frame=frame;
    }
    
    public JFrame getFrame() {
        return this.frame;
    }
    
    public Object getTO() throws IGFormatoException {
        ig.formato.dto.EntidadDTO dto=new ig.formato.dto.EntidadDTO();
        dto.setNombre(this.TNombre.getText().trim());
        dto.setCodigo(this.TCodigo.getText().trim());
//        dto.setGrupoPortafolio(Integer.valueOf(this.TGrupoPoratafolio.getText()));
//        dto.setIdConexion(((ig.formato.dto.ConexionDTO)this.CBConexiones.getSelectedItem()).getId());
        dto.setPalabraClave(this.TClave.getText().trim());
//        dto.setPatrimonioFideicomiso(this.CPatrimonioFideicomiso.isSelected()?1:0);
        dto.setTipo(this.TTipo.getText().trim());
        return dto;
    }
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Eliminar;
    private javax.swing.JButton Nuevo;
    private javax.swing.JTable TCampos;
    private javax.swing.JTextField TClave;
    private javax.swing.JTextField TCodigo;
    private javax.swing.JTextField TNombre;
    private javax.swing.JTextField TTipo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables
    
}
